﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using PS.Practices.SiteBuilder.Features.Schema.DOM;

namespace PS.Practices.Provisioner.Provisioner.Schema.Logic
{
    public class OmListOperationVisitorHelper
    {
        public static void ProcessItem(ListItem item, SPList list, SPWeb web)
        {
            if (item.ID > 0)
            {
                try
                {
                    SPListItem listItem = list.Items.GetItemById(item.ID);
                    if (listItem != null)
                    {
                        item.FixupItemACL.Execute(web, list, listItem);
                    }
                }
                catch (ArgumentException)
                { }
            }
        }

        public static void ProcessFile(ListItem listItem, SPList list, SPWeb web)
        {
            if (!string.IsNullOrEmpty(listItem.Name))
            {
                foreach (SPListItem item in list.Items)
                    if (item.File != null && item.File.Name == listItem.Name)
                    {
                        if (listItem.FixupItemACL != null)
                            listItem.FixupItemACL.Execute(web, list, item);

                        if (listItem.FileStatus != null)
                            listItem.FileStatus.Execute(web, item.File);
                    }
            }
        }

        public static void ProcessFolder(ListItem item, SPList list, SPWeb web)
        {
            if (!string.IsNullOrEmpty(item.Name))
            {

                foreach (SPListItem listItem in list.Folders)
                {
                    SPFolder folder = listItem.Folder;
                    if (folder != null && folder.Name == item.Name && folder.Item != null)
                    {
                        item.FixupItemACL.Execute(web, list, folder.Item);
                        break;
                    }
                }
            }
        }

    }
}
